﻿using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using DBUtility;
namespace OracleDAL.Room
{
    /// <summary>
    /// 数据访问类ROOM_Environment。
    /// </summary>
    public class ROOM_Environment
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public ROOM_Environment()
        { }
        #region  成员方法
        /// <summary>
        /// 实例化机房环境对象
        /// </summary>
        /// <param name="ROOMID">机房ID</param>
        /// <returns></returns>
        public Model.Room.ROOM_Environment GetModel(string ROOMID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select RL.ROOMID,RL.ROOMNAME,EY.COLLECTID,EY.SHOWNAME,EY.YXYC_VALUE, ");
            strSql.Append(" (select CODEVALUE  from CODE where RL.BELONGAREA=CODEID) AS BELONGAREANAME, ");
            strSql.Append(" RL.BELONGAREA,RL.REMARK,RL.BACKGROUNDIMAGE,RL.ROOMIMAGE,RL.ADDRESS,RL.LAT,RL.LNG,EY.INSERTDATE ");
            strSql.Append(" from ROOM_LIST RL,EQUIP_OUT_CONFIG EOC,ENV_YXYC EY ");
            strSql.Append(" where  RL.ROOMID=:ROOMID and EOC.STATIONNO=RL.ROOMID and EOC.EQUIPNO=EY.EQUIPNO ");
            strSql.Append(" order by RL.BELONGAREA,RL.ROOMID,EY.SHOWNAME ");
            //strSql.Append("select RL.ROOMID,RL.ROOMNAME,RL.ROOMIMAGE,EY.COLLECTID,EY.SHOWNAME,EY.YXYC_VALUE,EY.INSERTDATE from ROOM_LIST RL,EQUIP_OUT_CONFIG EOC,ENV_YXYC EY ");
            //strSql.Append(" where RL.ROOMID=:ROOMID and EOC.STATIONNO=RL.ROOMID and EOC.EQUIPNO=EY.EQUIPNO ");
            OracleParameter[] parameters = {
					new OracleParameter(":ROOMID", OracleType.VarChar,25)};
            parameters[0].Value = ROOMID;
            DataSet ds = OracleHelper.Query(strSql.ToString(), parameters);
            if (ds != null)
            {
                return GetModelData(ds.Tables[0]);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获取机房动力环境数据
        /// </summary>
        /// <param name="DT">某一个机房动力环境的数据</param>
        /// <returns></returns>
        public Model.Room.ROOM_Environment GetModelData(DataTable DT)
        {
            Model.Room.ROOM_Environment model = new Model.Room.ROOM_Environment();
            if (DT.Rows.Count > 0)
            {
                object it = new object();
                model.ROOMID = DT.Rows[0]["ROOMID"].ToString();
                model.ROOMNAME = DT.Rows[0]["ROOMNAME"].ToString();
                model.COLLECTID = DT.Rows[0]["COLLECTID"].ToString();
                model.ROOMIMAGE = DT.Rows[0]["ROOMIMAGE"].ToString();
                int length = DT.Rows.Count;
                for (int i = 0; i < length; i++)
                {
                    if (i == 0 || (DateTime)DT.Rows[i]["INSERTDATE"] > (DateTime)it)
                    {
                        it = DT.Rows[i]["INSERTDATE"];
                    }
                    //model.INSERTDATE = DT.Rows[i]["INSERTDATE"].ToString();
                    switch (DT.Rows[i]["SHOWNAME"].ToString())
                    {
                        case "温度":
                            model.TEMPERATURE = DT.Rows[i]["YXYC_VALUE"].ToString();
                            break;
                        case "湿度":
                            model.HUMIDITY = DT.Rows[i]["YXYC_VALUE"].ToString();
                            break;
                        case "烟感":
                            if (DT.Rows[i]["YXYC_VALUE"].ToString().Length == 0)
                            {
                                model.SOOT = "未配置";
                            }
                            else
                            {
                                model.SOOT = DT.Rows[i]["YXYC_VALUE"].ToString() == "0" ? "异常" : "正常";
                            }

                            break;
                        case "水浸":
                            if (DT.Rows[i]["YXYC_VALUE"].ToString().Length == 0)
                            {
                                model.INUNDATE = "未配置";
                            }
                            else
                            {
                                model.INUNDATE = DT.Rows[i]["YXYC_VALUE"].ToString() == "0" ? "异常" : "正常";
                            }
                            break;
                        case "A相电压":
                            model.AVOLTAGE = DT.Rows[i]["YXYC_VALUE"].ToString();
                            break;
                        case "B相电压":
                            model.BVOLTAGE = DT.Rows[i]["YXYC_VALUE"].ToString();
                            break;
                        case "C相电压":
                            model.CVOLTAGE = DT.Rows[i]["YXYC_VALUE"].ToString();
                            break;
                        case "A相电流":
                            model.AELECTRICCURRENT = DT.Rows[i]["YXYC_VALUE"].ToString();
                            break;
                        case "B相电流":
                            model.BELECTRICCURRENT = DT.Rows[i]["YXYC_VALUE"].ToString();
                            break;
                        case "C相电流":
                            model.CELECTRICCURRENT = DT.Rows[i]["YXYC_VALUE"].ToString();
                            break;
                        default:
                            break;
                    }
                }
                model.INSERTDATE = it.ToString();
                return model;
            }
            else
            {
                return null;
            }

        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RL.ROOMID,RL.ROOMNAME,EY.COLLECTID,EY.SHOWNAME,EY.YXYC_VALUE,EY.INSERTDATE from ROOM_LIST RL,EQUIP_OUT_CONFIG EOC,ENV_YXYC EY ");
            strSql.Append(" where EOC.STATIONNO=RL.ROOMID and EOC.EQUIPNO=EY.EQUIPNO ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" and " + strWhere);
            }
            strSql.Append(" order by RL.ROOMID,EY.SHOWNAME");
            return OracleHelper.Query(strSql.ToString());
        }
        /// <summary>
        /// 获取机房信息
        /// </summary>
        /// <returns></returns>
        public List<Model.Room.ROOM_INFO> GetRoomInfo()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select RL.ROOMID,RL.ROOMNAME,EY.COLLECTID,EY.SHOWNAME,EY.YXYC_VALUE, ");
            strSql.Append(" (select CODEVALUE  from CODE where RL.BELONGAREA=CODEID) AS BELONGAREANAME, ");
            strSql.Append(" RL.BELONGAREA,RL.REMARK,RL.BACKGROUNDIMAGE,RL.ROOMIMAGE,RL.ADDRESS,RL.LAT,RL.LNG,EY.INSERTDATE ");
            strSql.Append(" from ROOM_LIST RL,EQUIP_OUT_CONFIG EOC,ENV_YXYC EY ");
            strSql.Append(" where  RL.ROOMID=EOC.STATIONNO and EOC.EQUIPNO=EY.EQUIPNO ");
            strSql.Append(" order by RL.BELONGAREA,RL.ROOMID,EY.SHOWNAME ");
            DataSet ds = OracleHelper.Query(strSql.ToString());
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                List<Model.Room.ROOM_INFO> modelList = new List<Model.Room.ROOM_INFO>();
                Model.Room.ROOM_INFO model = null;
                int length = ds.Tables[0].Rows.Count;
                string roomid = string.Empty;
                string roomidtmp = string.Empty;
                for (int i = 0; i < length; i++)
                {
                    roomidtmp = ds.Tables[0].Rows[i]["ROOMID"].ToString();
                    if (0 == i)
                    {
                        roomid = roomidtmp;
                        model = GetRoomEnv(ds.Tables[0].Select(" roomid='" + roomid + "'"));
                        modelList.Add(model);
                    }
                    if (roomidtmp == roomid)//如果相同就说明已经初始化过，直接跳过
                    {
                        continue;
                    }
                    else
                    {
                        roomid = roomidtmp;
                        model = GetRoomEnv(ds.Tables[0].Select(" roomid='" + roomid + "'"));
                        modelList.Add(model);
                    }
                }
                return modelList;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获取机房信息
        /// </summary>
        /// <param name="ROOMID">机房ID</param>
        /// <returns></returns>
        public Model.Room.ROOM_INFO GetRoomInfo(string ROOMID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select RL.ROOMID,RL.ROOMNAME,EY.COLLECTID,EY.SHOWNAME,EY.YXYC_VALUE, ");
            strSql.Append(" (select CODEVALUE  from CODE where RL.BELONGAREA=CODEID) AS BELONGAREANAME, ");
            strSql.Append(" RL.BELONGAREA,RL.REMARK,RL.BACKGROUNDIMAGE,RL.ROOMIMAGE,RL.ADDRESS,RL.LAT,RL.LNG,EY.INSERTDATE ");
            strSql.Append(" from ROOM_LIST RL,EQUIP_OUT_CONFIG EOC,ENV_YXYC EY ");
            strSql.Append(" where  RL.ROOMID=:ROOMID and EOC.STATIONNO=RL.ROOMID and EOC.EQUIPNO=EY.EQUIPNO ");
            strSql.Append(" order by RL.BELONGAREA,RL.ROOMID,EY.SHOWNAME ");
            OracleParameter[] parameters = {
					new OracleParameter(":ROOMID", OracleType.VarChar,25)};
            parameters[0].Value = ROOMID;

            DataSet ds = OracleHelper.Query(strSql.ToString(), parameters);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                Model.Room.ROOM_INFO model = new Model.Room.ROOM_INFO();
                model = GetRoomEnv(ds.Tables[0].Rows);
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获取机房信息
        /// </summary>
        /// <param name="ROOMID">机房ID</param>
        /// <returns></returns>
        public Model.Room.ROOM_INFO GetRoomInfoByROOMID(string ROOMID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select EYT.ROOMNAME,EYT.TEMPERATURE,EYT.HUMIDITY,EYT.SOOT,EYT.INUNDATE,EYT.AVOLTAGE,EYT.BVOLTAGE, ");
            strSql.Append(" EYT.CVOLTAGE,EYT.AELECTRICCURRENT,EYT.BELECTRICCURRENT,EYT.CELECTRICCURRENT,EYT.ROOMID,EYT.INSERTTIME, ");
            strSql.Append(" (select CODEVALUE  from CODE where RL.BELONGAREA=CODEID) AS BELONGAREANAME, ");
            strSql.Append(" RL.BELONGAREA,RL.REMARK,RL.BACKGROUNDIMAGE,RL.ROOMIMAGE,RL.ADDRESS,RL.LAT,RL.LNG ");
            strSql.Append(" from ENV_YXYC_TMP EYT,ROOM_LIST RL");
            strSql.Append(" where EYT.ROOMID=:ROOMID and EYT.ROOMID=RL.ROOMID");
            OracleParameter[] parameters = {
					new OracleParameter(":ROOMID", OracleType.VarChar,25)};
            parameters[0].Value = ROOMID;

            DataSet ds = OracleHelper.Query(strSql.ToString(), parameters);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                Model.Room.ROOM_INFO model = new Model.Room.ROOM_INFO();
                model = GetRoomEnvByROOMID(ds.Tables[0].Rows);
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 从行集中获取机房动力环境数据
        /// </summary>
        /// <param name="dr">行集</param>
        /// <returns></returns>
        public Model.Room.ROOM_INFO GetRoomEnvByROOMID(DataRowCollection dr)
        {
            if (dr.Count > 0)
            {
                object it = new object();
                Model.Room.ROOM_INFO modelInfo = new Model.Room.ROOM_INFO();
                Model.Room.ROOM_LIST modelRoomlist = new Model.Room.ROOM_LIST();
                Model.Room.ROOM_Environment modelEnv = new Model.Room.ROOM_Environment();
                //   int length = dr.Length;
                modelRoomlist.ROOMID = dr[0]["ROOMID"].ToString();
                modelRoomlist.ROOMNAME = dr[0]["ROOMNAME"].ToString();
                // modelRoomlist.COLLECTID = dr[0]["COLLECTID"].ToString();
                modelRoomlist.BELONGAREA = dr[0]["BELONGAREA"].ToString();
                // modelRoomlist.BELONGAREANAME = dr[0]["BELONGAREANAME"].ToString();
                modelRoomlist.ADDRESS = dr[0]["ADDRESS"].ToString();
                modelRoomlist.BACKGROUNDIMAGE = dr[0]["BACKGROUNDIMAGE"].ToString();
                modelRoomlist.LAT = dr[0]["LAT"].ToString() == "" ? Convert.ToDecimal("0.00000") : Convert.ToDecimal(dr[0]["LAT"].ToString());
                modelRoomlist.LNG = dr[0]["LNG"].ToString() == "" ? Convert.ToDecimal("0.00000") : Convert.ToDecimal(dr[0]["LNG"].ToString());
                modelRoomlist.REMARK = dr[0]["REMARK"].ToString();
                modelRoomlist.ROOMIMAGE = dr[0]["ROOMIMAGE"].ToString();

                modelEnv.TEMPERATURE = dr[0]["TEMPERATURE"].ToString();
                modelEnv.HUMIDITY = dr[0]["HUMIDITY"].ToString();
                modelEnv.SOOT = dr[0]["SOOT"].ToString();
                modelEnv.INUNDATE = dr[0]["INUNDATE"].ToString();
                modelEnv.AVOLTAGE = dr[0]["AVOLTAGE"].ToString();
                modelEnv.BVOLTAGE = dr[0]["BVOLTAGE"].ToString();
                modelEnv.CVOLTAGE = dr[0]["CVOLTAGE"].ToString();
                modelEnv.AELECTRICCURRENT = dr[0]["AELECTRICCURRENT"].ToString();
                modelEnv.BELECTRICCURRENT = dr[0]["BELECTRICCURRENT"].ToString();
                modelEnv.CELECTRICCURRENT = dr[0]["CELECTRICCURRENT"].ToString();
                modelEnv.INSERTDATE = dr[0]["INSERTTIME"].ToString();
                modelInfo.ROOMENV = modelEnv;
                modelInfo.ROOMLIST = modelRoomlist;

                return modelInfo;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 从行集中获取机房动力环境数据
        /// </summary>
        /// <param name="dr">行集</param>
        /// <returns></returns>
        public Model.Room.ROOM_INFO GetRoomEnv(DataRow[] dr)
        {
            if (dr.Length > 0)
            {
                object it = new object();
                Model.Room.ROOM_INFO modelInfo = new Model.Room.ROOM_INFO();
                Model.Room.ROOM_LIST modelRoomlist = new Model.Room.ROOM_LIST();
                Model.Room.ROOM_Environment modelEnv = new Model.Room.ROOM_Environment();
                int length = dr.Length;
                modelRoomlist.ROOMID = dr[0]["ROOMID"].ToString();
                modelRoomlist.ROOMNAME = dr[0]["ROOMNAME"].ToString();
                // modelRoomlist.COLLECTID = dr[0]["COLLECTID"].ToString();
                modelRoomlist.BELONGAREA = dr[0]["BELONGAREA"].ToString();
                // modelRoomlist.BELONGAREANAME = dr[0]["BELONGAREANAME"].ToString();
                modelRoomlist.ADDRESS = dr[0]["ADDRESS"].ToString();
                modelRoomlist.BACKGROUNDIMAGE = dr[0]["BACKGROUNDIMAGE"].ToString();
                modelRoomlist.LAT = dr[0]["LAT"].ToString() == "" ? Convert.ToDecimal("0.00000") : Convert.ToDecimal(dr[0]["LAT"].ToString());
                modelRoomlist.LNG = dr[0]["LNG"].ToString() == "" ? Convert.ToDecimal("0.00000") : Convert.ToDecimal(dr[0]["LNG"].ToString());
                modelRoomlist.REMARK = dr[0]["REMARK"].ToString();
                modelRoomlist.ROOMIMAGE = dr[0]["ROOMIMAGE"].ToString();
                for (int i = 0; i < length; i++)
                {
                    if (i == 0 || (DateTime)dr[i]["INSERTDATE"] > (DateTime)it)
                    {
                        it = dr[i]["INSERTDATE"];
                    }
                    // modelEnv.INSERTDATE = dr[i]["INSERTDATE"].ToString();
                    switch (dr[i]["SHOWNAME"].ToString())
                    {
                        case "温度":
                            modelEnv.TEMPERATURE = dr[i]["YXYC_VALUE"].ToString();
                            break;
                        case "湿度":
                            modelEnv.HUMIDITY = dr[i]["YXYC_VALUE"].ToString();
                            break;
                        case "烟感":
                            if (dr[i]["YXYC_VALUE"].ToString().Length == 0)
                            {
                                modelEnv.SOOT = "未配置";
                            }
                            else
                            {
                                modelEnv.SOOT = dr[i]["YXYC_VALUE"].ToString() == "0" ? "异常" : "正常";
                            }
                            break;
                        case "水浸":
                            if (dr[i]["YXYC_VALUE"].ToString().Length == 0)
                            {
                                modelEnv.INUNDATE = "未配置";
                            }
                            else
                            {
                                modelEnv.INUNDATE = dr[i]["YXYC_VALUE"].ToString() == "0" ? "异常" : "正常";
                            }
                            break;
                        case "A相电压":
                            modelEnv.AVOLTAGE = dr[i]["YXYC_VALUE"].ToString();
                            break;
                        case "B相电压":
                            modelEnv.BVOLTAGE = dr[i]["YXYC_VALUE"].ToString();
                            break;
                        case "C相电压":
                            modelEnv.CVOLTAGE = dr[i]["YXYC_VALUE"].ToString();
                            break;
                        case "A相电流":
                            modelEnv.AELECTRICCURRENT = dr[i]["YXYC_VALUE"].ToString();
                            break;
                        case "B相电流":
                            modelEnv.BELECTRICCURRENT = dr[i]["YXYC_VALUE"].ToString();
                            break;
                        case "C相电流":
                            modelEnv.CELECTRICCURRENT = dr[i]["YXYC_VALUE"].ToString();
                            break;
                        default:
                            break;
                    }
                }
                modelEnv.INSERTDATE = it.ToString();
                modelInfo.ROOMENV = modelEnv;
                modelInfo.ROOMLIST = modelRoomlist;

                return modelInfo;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 从行集中获取机房动力环境数据
        /// </summary>
        /// <param name="drc">行集</param>
        /// <returns></returns>
        public Model.Room.ROOM_INFO GetRoomEnv(DataRowCollection drc)
        {
            if (drc.Count > 0)
            {
                object it = new object();
                Model.Room.ROOM_INFO modelInfo = new Model.Room.ROOM_INFO();
                Model.Room.ROOM_LIST modelRoomlist = new Model.Room.ROOM_LIST();
                Model.Room.ROOM_Environment modelEnv = new Model.Room.ROOM_Environment();
                int length = drc.Count;
                modelRoomlist.ROOMID = drc[0]["ROOMID"].ToString();
                modelRoomlist.ROOMNAME = drc[0]["ROOMNAME"].ToString();
                //modelRoomlist.COLLECTID = drc[0]["COLLECTID"].ToString();
                modelRoomlist.BELONGAREA = drc[0]["BELONGAREA"].ToString();
                //modelRoomlist.BELONGAREA = drc[0]["BELONGAREA"].ToString();
                modelRoomlist.ADDRESS = drc[0]["ADDRESS"].ToString();
                modelRoomlist.BACKGROUNDIMAGE = drc[0]["BACKGROUNDIMAGE"].ToString();
                modelRoomlist.LAT = Convert.ToDecimal(drc[0]["LAT"].ToString());
                modelRoomlist.LNG = Convert.ToDecimal(drc[0]["LNG"].ToString());
                modelRoomlist.REMARK = drc[0]["REMARK"].ToString();
                modelRoomlist.ROOMIMAGE = drc[0]["ROOMIMAGE"].ToString();
                for (int i = 0; i < length; i++)
                {
                    if (i == 0 || (DateTime)drc[i]["INSERTDATE"] > (DateTime)it)
                    {
                        it = drc[i]["INSERTDATE"];
                    }
                    //modelEnv.INSERTDATE = drc[i]["INSERTDATE"].ToString();
                    switch (drc[i]["SHOWNAME"].ToString())
                    {
                        case "温度":
                            modelEnv.TEMPERATURE = drc[i]["YXYC_VALUE"].ToString();
                            break;
                        case "湿度":
                            modelEnv.HUMIDITY = drc[i]["YXYC_VALUE"].ToString();
                            break;
                        case "烟感":
                            if (drc[i]["YXYC_VALUE"].ToString().Length == 0)
                            {
                                modelEnv.SOOT = "未配置";
                            }
                            else
                            {
                                modelEnv.SOOT = drc[i]["YXYC_VALUE"].ToString() == "0" ? "异常" : "正常";
                            }
                            break;
                        case "水浸":
                            if (drc[i]["YXYC_VALUE"].ToString().Length == 0)
                            {
                                modelEnv.INUNDATE = "未配置";
                            }
                            else
                            {
                                modelEnv.INUNDATE = drc[i]["YXYC_VALUE"].ToString() == "0" ? "异常" : "正常";
                            }
                            break;
                        case "A相电压":
                            modelEnv.AVOLTAGE = drc[i]["YXYC_VALUE"].ToString();
                            break;
                        case "B相电压":
                            modelEnv.BVOLTAGE = drc[i]["YXYC_VALUE"].ToString();
                            break;
                        case "C相电压":
                            modelEnv.CVOLTAGE = drc[i]["YXYC_VALUE"].ToString();
                            break;
                        case "A相电流":
                            modelEnv.AELECTRICCURRENT = drc[i]["YXYC_VALUE"].ToString();
                            break;
                        case "B相电流":
                            modelEnv.BELECTRICCURRENT = drc[i]["YXYC_VALUE"].ToString();
                            break;
                        case "C相电流":
                            modelEnv.CELECTRICCURRENT = drc[i]["YXYC_VALUE"].ToString();
                            break;
                        default:
                            break;
                    }
                }
                modelEnv.INSERTDATE = it.ToString();
                modelInfo.ROOMENV = modelEnv;
                modelInfo.ROOMLIST = modelRoomlist;

                return modelInfo;
            }
            else
            {
                return null;
            }
        }
        #endregion  成员方法
    }
}


